//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyq.bean.mySql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ElectC_Sql {
    public ElectC_Sql() {
    }

    public Boolean electC_Sql(String cNo, String cname, String sNo) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        Boolean bool = false;
        if (conn != null) {
            try {
                String sql = "insert into electcourse (C_No,C_Name,S_No,Grade) values(?,?,?,?)";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, cNo);
                ps.setString(2, cname);
                ps.setString(3, sNo);
                ps.setString(4, "0");//默认为0
                int count = ps.executeUpdate();
                if (count >= 1) {
                    bool = true;
                }
            } catch (Exception var10) {
                Exception e = var10;
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return bool;
    }



    public boolean cancelCourseAssignment(String courseId) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        boolean bool = false;

        if (conn != null) {
            try {
                // 根据课程编号删除对应的选课记录
                String sql = "DELETE FROM electcourse WHERE C_No = ?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, courseId);
                int count = ps.executeUpdate();

                // 如果删除成功（影响行数>=0），返回true
                if (count >= 0) {
                    bool = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return bool;
    }

    // 可选：根据课程编号和学生编号删除特定记录
    public Boolean cancelStudentCourseAssignment(String courseId, String studentId) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        Boolean bool = false;

        if (conn != null) {
            try {
                // 根据课程编号和学生编号删除特定的选课记录
                String sql = "DELETE FROM electcourse WHERE C_No = ? AND S_No = ?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, courseId);
                ps.setString(2, studentId);
                int count = ps.executeUpdate();

                // 如果删除成功（影响行数>=0），返回true
                if (count >= 0) {
                    bool = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return bool;
    }


    public boolean isCourseAssigned(String courseId) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        boolean isAssigned = false;

        if (conn != null) {
            try {
                // 查询是否存在该课程的选课记录
                String sql = "SELECT COUNT(*) FROM electcourse WHERE C_No = ?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, courseId);
                ResultSet rs = ps.executeQuery();

                if (rs.next()) {
                    int count = rs.getInt(1);
                    // 如果记录数大于0，说明课程已被分配
                    if (count > 0) {
                        isAssigned = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return isAssigned;
    }
}
